﻿using WF = CPPEI.WorkflowCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace Demo
{
    public class Global : System.Web.HttpApplication
    {
        public static string WF_PAGEREDIRECT = "WF_PAGEDIRECT";
        public static string WF_BASEAUDITANDREJECT = "WF_BASEAUDITANDREJECT";
        public static string WF_AUDITANDREJECTWITHACTORS = "WF_AUDITANDREJECTWITHACTORS";

        protected void Application_Start(object sender, EventArgs e)
        {
            WF.Engine.Config(new WF.Simulator.WorkflowStorage(),
                new WF.Simulator.InstanceStorage(),
                new WF.Simulator.ActionRecordStorage());

            // 配置
            RegisterWF1();
            RegisterWF_BASEAUDITANDREJECT();
            RegisterWF_AUDITANDREJECTWITHACTORS();
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }

        private void RegisterWF1()
        {
            WF.Workflow workflow = new WF.Workflow(WF_PAGEREDIRECT, new Version(1, 0));

            // step1:submit
            WF.Page page1 = new WF.Page();
            page1.Address = "~/PageRedirect/Submit.aspx";
            WF.Activity submitActivity1 = new WF.Activity("SUBMIT", "提交", WF.ActivityType.Begin, WF.ActivityTemplateType.Submit);
            submitActivity1.Actions.Add(new WF.Action("SUBMIT1", "提交1", WF.ActionType.Submit));
            submitActivity1.Property.Page = page1;

            // step2:audit
            WF.Page page2 = new WF.Page();
            page2.Address = "~/PageRedirect/Audit.aspx";
            WF.Activity submitActivity2 = new WF.Activity("SUBMIT", "提交", WF.ActivityType.Process, WF.ActivityTemplateType.Submit);
            submitActivity2.Actions.Add(new WF.Action("SUBMIT2", "提交2", WF.ActionType.Submit));
            submitActivity2.Property.Page = page2;

            // step3:submit
            WF.Page page3 = new WF.Page();
            page3.Address = "~/PageRedirect/Submit.aspx";
            WF.Activity auditActivity1 = new WF.Activity("AUDIT", "审核", WF.ActivityType.Process, WF.ActivityTemplateType.Submit);
            auditActivity1.Actions.Add(new WF.Action("APPROVE1", "通过1", WF.ActionType.Submit));
            auditActivity1.Property.Page = page3;

            // step4:audit
            WF.Page page4 = new WF.Page();
            page4.Address = "~/PageRedirect/Audit.aspx";
            WF.Activity auditActivity2 = new WF.Activity("AUDIT", "审核", WF.ActivityType.Process, WF.ActivityTemplateType.Submit);
            auditActivity2.Actions.Add(new WF.Action("APPROVE2", "通过2", WF.ActionType.Submit));
            auditActivity2.Property.Page = page4;

            // step5:finish
            WF.Page page5 = new WF.Page();
            page5.Address = "~/PageRedirect/Finish.aspx";
            WF.Activity finishActivity = new WF.Activity("FINISH", "完结", WF.ActivityType.End, WF.ActivityTemplateType.Submit);
            finishActivity.Property.Page = page5;


            workflow.Root = submitActivity1;

            submitActivity1.Next.Add(new WF.Link(submitActivity1.Actions[0], auditActivity1));
            auditActivity1.Next.Add(new WF.Link(submitActivity1.Actions[0], submitActivity2));
            submitActivity2.Next.Add(new WF.Link(submitActivity1.Actions[0], auditActivity2));

            WF.Engine.WorkflowStorage.Save(workflow);
        }

        private void RegisterWF_BASEAUDITANDREJECT()
        {
            WF.Workflow workflow = new WF.Workflow(WF_BASEAUDITANDREJECT, new Version(1, 0));

            WF.Activity submitActivity1 = new WF.Activity("SUBMIT", "提交", WF.ActivityType.Begin, WF.ActivityTemplateType.Submit);
            submitActivity1.Actions.Add(new WF.Action("SUBMIT1", "提交1", WF.ActionType.Submit));

            WF.Activity auditActivity1 = new WF.Activity("AUDIT", "审核", WF.ActivityType.Process, WF.ActivityTemplateType.Submit);
            auditActivity1.Actions.Add(new WF.Action("APPROVE1", "通过1", WF.ActionType.Approve));
            auditActivity1.Actions.Add(new WF.Action("REJECT1", "退回1", WF.ActionType.Reject));

            WF.Activity auditActivity2 = new WF.Activity("AUDIT", "审核", WF.ActivityType.Process, WF.ActivityTemplateType.Submit);
            auditActivity2.Actions.Add(new WF.Action("APPROVE2", "通过2", WF.ActionType.Approve));
            auditActivity2.Actions.Add(new WF.Action("REJECT2", "退回2", WF.ActionType.Reject));

            WF.Activity finishActivity = new WF.Activity("FINISH", "完结", WF.ActivityType.End, WF.ActivityTemplateType.Submit);

            // step1:submit
            WF.Page page1 = new WF.Page();
            page1.Address = "~/BaseAuditAndReject/Submit.aspx";
            submitActivity1.Property.Page = page1;

            // step2:audit
            WF.Page page2 = new WF.Page();
            page2.Address = "~/BaseAuditAndReject/Audit.aspx";
            auditActivity1.Property.Page = page2;

            // step3:audit
            WF.Page page3 = new WF.Page();
            page3.Address = "~/BaseAuditAndReject/Audit.aspx";
            auditActivity2.Property.Page = page2;

            // step4:finish
            WF.Page page4 = new WF.Page();
            page4.Address = "~/BaseAuditAndReject/Finish.aspx";
            finishActivity.Property.Page = page4;

            workflow.Root = submitActivity1;

            submitActivity1.Next.Add(new WF.Link(submitActivity1.Actions[0], auditActivity1));

            auditActivity1.Next.Add(new WF.Link(auditActivity1.Actions[0], auditActivity2));
            auditActivity1.Next.Add(new WF.Link(auditActivity1.Actions[1], submitActivity1));

            auditActivity2.Next.Add(new WF.Link(auditActivity2.Actions[0], finishActivity));
            auditActivity2.Next.Add(new WF.Link(auditActivity2.Actions[1], submitActivity1));

            WF.Engine.WorkflowStorage.Save(workflow);
        }

        private void RegisterWF_AUDITANDREJECTWITHACTORS()
        {
            WF.Workflow workflow = new WF.Workflow(WF_AUDITANDREJECTWITHACTORS, new Version(1, 0));

            WF.Activity submitActivity1 = new WF.Activity("SUBMIT", "提交", WF.ActivityType.Begin, WF.ActivityTemplateType.Submit);
            submitActivity1.Actions.Add(new WF.Action("SUBMIT1", "提交1", WF.ActionType.Submit));

            WF.Activity auditActivity1 = new WF.Activity("AUDIT", "审核", WF.ActivityType.Process, WF.ActivityTemplateType.Submit);
            auditActivity1.Actions.Add(new WF.Action("APPROVE1", "通过1", WF.ActionType.Submit));
            auditActivity1.Actions.Add(new WF.Action("REJECT1", "退回1", WF.ActionType.Submit));

            WF.Activity auditActivity2 = new WF.Activity("AUDIT", "审核", WF.ActivityType.Process, WF.ActivityTemplateType.Submit);
            auditActivity2.Actions.Add(new WF.Action("APPROVE2", "通过2", WF.ActionType.Submit));
            auditActivity2.Actions.Add(new WF.Action("REJECT2", "退回2", WF.ActionType.Submit));

            WF.Activity finishActivity = new WF.Activity("FINISH", "完结", WF.ActivityType.End, WF.ActivityTemplateType.Submit);

            // step1:submit
            WF.Page page1 = new WF.Page();
            page1.Address = "~/AUDITANDREJECTWITHACTORS/Submit.aspx";
            submitActivity1.Property.Page = page1;

            // step2:audit
            WF.Page page2 = new WF.Page();
            page2.Address = "~/AUDITANDREJECTWITHACTORS/Audit.aspx";
            auditActivity1.Property.Page = page2;
            auditActivity1.Actors.Add(new WF.Actor("user02", "用户02", WF.ActorType.User));
            auditActivity1.Actors.Add(new WF.Actor("user03", "用户03", WF.ActorType.User));

            // step3:audit
            WF.Page page3 = new WF.Page();
            page3.Address = "~/AUDITANDREJECTWITHACTORS/Audit.aspx";
            auditActivity2.Property.Page = page2;
            auditActivity2.Actors.Add(new WF.Actor("user04", "用户04", WF.ActorType.User));
            auditActivity2.Actors.Add(new WF.Actor("user05", "用户05", WF.ActorType.User));

            // step4:finish
            WF.Page page4 = new WF.Page();
            page4.Address = "~/AUDITANDREJECTWITHACTORS/Finish.aspx";
            finishActivity.Property.Page = page4;

            workflow.Root = submitActivity1;

            submitActivity1.Next.Add(new WF.Link(submitActivity1.Actions[0], auditActivity1));

            auditActivity1.Next.Add(new WF.Link(auditActivity1.Actions[0], auditActivity2));
            auditActivity1.Next.Add(new WF.Link(auditActivity1.Actions[1], submitActivity1));

            auditActivity2.Next.Add(new WF.Link(auditActivity2.Actions[0], finishActivity));
            auditActivity2.Next.Add(new WF.Link(auditActivity2.Actions[1], submitActivity1));

            WF.Engine.WorkflowStorage.Save(workflow);
        }
    }
}